<?php

namespace App\Http\Controllers\Index;

use App\Models\Index\LiveMovie;
use App\Models\Index\LiveMovieCate;
use App\Models\User\LiveUserCollect;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MovieController extends Controller
{
    public function cateList(Request $request)
    {
        $attribute  = (int)$request->input('attribute',1);
        $data       = LiveMovieCate::getCateList($attribute);
        return self::response($data,'200');
    }

    public function hot(Request $request)
    {
        $page       = (int)$request->input('page');
        $attribute  = (int)$request->input('attribute',1);
        if (empty($page)) {
            return self::response(null,-1,'页码不能为空');
        }
        $data       = LiveMovie::getHotMovieList($attribute);
        return self::response($data,'200');
    }

    public function detail(Request $request)
    {
        $id         = (int)$request->input('id');
        $user_id    = (int)$request->input('user_id');
        if (empty($id)) {
            return self::response(null,-1,'电影ID不能为空');
        }
        if (empty($user_id)) {
            return self::response(null,-2,'用户不存在哦~');
        }
        $data = LiveMovie::getAppointMovie($id,$user_id);
        if (empty($data)) {
            return self::response(null,-2,'电影不存在');
        }
        return self::response($data,'200');
    }

    public function recommend(Request $request)
    {
        $id         = (int)$request->input('id');
        $page       = (int)$request->input('page');
        $cate_id    = (int)$request->input('cate_id');
        if (empty($id)) {
            return self::response(null,-1,'视频ID不能为空');
        }
        if (empty($page)) {
            return self::response(null,-2,'页码不能为空');
        }
        if (empty($cate_id)) {
            return self::response(null,-3,'分类不能为空');
        }
        $data = LiveMovie::getRecommendMovieList($cate_id);
        if (!empty($data['data'])) {
            $filtered = array_where($data['data'], function ($value, $key) use ($id) {
                return $value['id'] != $id;
            });
            $data['data'] = array_values($filtered);
        }
        return self::response($data,'200');
    }

    public function cate(Request $request)
    {
        $cate_id    = (int)$request->input('cate_id');
        $page       = (int)$request->input('page');
        if (empty($cate_id)) {
            return self::response(null,-1,'分类id不存在');
        }
        if (empty($page)) {
            return self::response(null,-2,'页码不能为空');
        }
        $data       = LiveMovie::getMovieByCateId($cate_id);
        return self::response($data,'200');
    }

    public function collect(Request $request)
    {
        $movie_id   = (int)$request->input('movie_id');
        $user_id    = (int)$request->input('user_id');
        $status     = (int)$request->input('status',0);
        if (empty($movie_id)) {
            return self::response(null,-1,'电影id不存在');
        }
        if (empty($user_id)) {
            return self::response(null,-2,'用户id不存在');
        }
        $flag = LiveUserCollect::createOrUpdate($user_id,$movie_id);
        if (!$flag) {
            return self::response(null,-3,'系统繁忙');
        }
        return self::response(['status' => $status == 1 ? 0 : 1],'200');
    }
}
